package vista;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import modelo.ModeloProducto;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VistaOrden extends javax.swing.JFrame {
	private JButton btnquitar;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JSpinner spncantidad;
	private JButton btnsalir;
	private JButton btnregistrar;
	private JButton btncancelar;
	private JTextField txtnombre;
	private JTextField txtcedula;
	private JTextField txtcodigo;
	private JLabel lblTotal;
	private JLabel jLabel9;
	private JLabel jLabel8;
	private JLabel jLabel7;
	private JLabel jLabel6;
	private JLabel jLabel2;
	private JTable tblIngrediente;
	private JLabel jLabel1;
	private JButton btnagregar;
	private JLabel jLabel3;
	private JTable tblAgregados;
	private DefaultTableModel modeloProducto,modeloAgregados;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				VistaOrden inst = new VistaOrden();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public VistaOrden() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Ordenes");
			{
				btnquitar = new JButton();
				getContentPane().add(btnquitar);
				btnquitar.setText("Quitar Prodcuto");
				btnquitar.setActionCommand("Quitar");
				btnquitar.setBounds(176, 329, 167, 28);
			}
			{
				modeloAgregados = 
					new DefaultTableModel(null,new String[] { "Producto", "Cantidad","Precio" });
				tblAgregados = new JTable();
				getContentPane().add(tblAgregados);
				tblAgregados.setModel(modeloAgregados);
				tblAgregados.setBounds(369, 199, 415, 185);
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setText("Cantidad");
				jLabel4.setBounds(523, 163, 138, 24);
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Prodcuto");
				jLabel3.setBounds(369, 163, 138, 24);
			}
			{
				btnagregar = new JButton();
				getContentPane().add(btnagregar);
				btnagregar.setText("Agredar Producto");
				btnagregar.setActionCommand("Agregar");
				btnagregar.setBounds(176, 278, 167, 30);
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Cantidad");
				jLabel1.setBounds(176, 198, 107, 19);
			}
			{
				modeloProducto = 
					new DefaultTableModel( null,
							new String[] { "Producto"});
				tblIngrediente = new JTable();
				getContentPane().add(tblIngrediente);
				tblIngrediente.setModel(modeloProducto);
				tblIngrediente.setBounds(14, 198, 138, 159);
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Productos Registrados ");
				jLabel2.setBounds(18, 165, 184, 21);
			}
			{
				jLabel5 = new JLabel();
				getContentPane().add(jLabel5);
				jLabel5.setText("Precio");
				jLabel5.setBounds(651, 163, 138, 24);
			}
			{
				jLabel6 = new JLabel();
				getContentPane().add(jLabel6);
				jLabel6.setText("Codigo");
				jLabel6.setBounds(217, 23, 107, 19);
			}
			{
				jLabel7 = new JLabel();
				getContentPane().add(jLabel7);
				jLabel7.setText("Nombre");
				jLabel7.setBounds(217, 120, 145, 15);
			}
			{
				jLabel8 = new JLabel();
				getContentPane().add(jLabel8);
				jLabel8.setText("Cedula");
				jLabel8.setBounds(217, 73, 107, 15);
			}
			{
				jLabel9 = new JLabel();
				getContentPane().add(jLabel9);
				jLabel9.setText("Total");
				jLabel9.setBounds(553, 406, 59, 15);
			}
			{
				lblTotal = new JLabel();
				getContentPane().add(lblTotal);
				lblTotal.setBounds(624, 406, 145, 15);
			}
			{
				txtcodigo = new JTextField();
				getContentPane().add(txtcodigo);
				txtcodigo.setBounds(303, 22, 115, 22);
			}
			{
				txtcedula = new JTextField();
				getContentPane().add(txtcedula);
				txtcedula.setBounds(302, 70, 119, 22);
			}
			{
				txtnombre = new JTextField();
				getContentPane().add(txtnombre);
				txtnombre.setBounds(302, 124, 113, 22);
			}
			{
				btncancelar = new JButton();
				getContentPane().add(btncancelar);
				btncancelar.setText("Cancelar");
				btncancelar.setActionCommand("Cancelar");
				btncancelar.setBounds(420, 451, 103, 24);
			}
			{
				btnregistrar = new JButton();
				getContentPane().add(btnregistrar);
				btnregistrar.setText("Registrar");
				btnregistrar.setBounds(188, 451, 103, 24);
			}
			{
				btnsalir = new JButton();
				getContentPane().add(btnsalir);
				btnsalir.setText("Salir");
				btnsalir.setActionCommand("Salir");
				btnsalir.setBounds(302, 451, 103, 24);
			}
			{
				Vector<Integer> v = new Vector<Integer>();
				int i =1;
				while(i<=100){
					v.add(i);
					i++;					
				}
				SpinnerListModel spncantidadModel = 
					new SpinnerListModel(
							new String[] { "Sun", "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" });
				spncantidad = new JSpinner();
				getContentPane().add(spncantidad);
				spncantidad.setModel(spncantidadModel);
				spncantidadModel.setList(v);
				spncantidad.setBounds(176, 236, 80, 22);
			}
			pack();
			this.setSize(790, 525);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	public void addlistener(ActionListener actionlistener){
    	btnagregar.addActionListener(actionlistener);
    	btncancelar.addActionListener(actionlistener);
    	btnquitar.addActionListener(actionlistener);
    	btnsalir.addActionListener(actionlistener);
    	btnregistrar.addActionListener(actionlistener);
    }

	
	public void limpiarCancelar(){
		txtcodigo.setText("");
		txtcedula.setText("");
		txtnombre.setText("");
		lblTotal.setText("");
		modeloAgregados.setRowCount(0);
		}
	

	public JTable getTblIngrediente() {
		return tblIngrediente;
	}


	public JTable getTblAgregados() {
		return tblAgregados;
	}

	

	public DefaultTableModel getModeloProducto() {
		return modeloProducto;
	}

	

	public DefaultTableModel getModeloAgregados() {
		return modeloAgregados;
	}


	public String getcodigo() {
		return txtcodigo.getText();
	}

	public String getnombre() {
		return txtnombre.getText();
	}
	
	public String getcedula() {
		return txtcedula.getText();
	}
	
	public Integer getCantidad() {
		return (Integer)spncantidad.getValue();
	}



	public String getTotal() {
		return lblTotal.getText();
	}

	public void quitar() {
		int i = tblAgregados.getSelectedRow();
		if(i!=-1){
			modeloAgregados.removeRow(i);
			calcularTotal();
		}
		else
			mostrarMensaje("Selecione el producto que desea quitar");
	}

	public void agregar(ModeloProducto m) {
		int j = tblIngrediente.getSelectedRow();
		if(j!=-1){
			int i =0;
			while(i< tblAgregados.getRowCount()) {
				if(tblAgregados.getValueAt(i,0).equals(tblIngrediente.getValueAt(i,0))){
					tblAgregados.setValueAt(getCantidad(), i, 1);
					calcularTotal();
					return;
				}			
				else
					i++;
			}
			if(i!=tblAgregados.getRowCount() || tblAgregados.getRowCount()==0){
				Vector fila = new Vector();
				fila.add(tblIngrediente.getValueAt(i,0));
				fila.add(getCantidad());
				fila.add(m.getPrecio());				
				modeloAgregados.addRow(fila);
			}
		calcularTotal();
		}
		else
			mostrarMensaje("Selecione el producto que desea agregar");
		
	}
	
	public void mostrarMensaje(String mensaje) {
		JOptionPane.showMessageDialog(this, mensaje);
	}
	
	public void calcularTotal() {
		int i =0;
		double total =0;
		while(i< tblAgregados.getRowCount()) {
			int c = (Integer) tblAgregados.getValueAt(i,1);
			double p = (Double)tblAgregados.getValueAt(i, 2); 
			total+=p*c;
			i++;
		}
		lblTotal.setText(""+total);
	}

	public JTextField getTxtcedula() {
		return txtcedula;
	}

	public void setTxtcedula(JTextField txtcedula) {
		this.txtcedula = txtcedula;
	}

	public void setTxtnombre(JTextField txtnombre) {
		this.txtnombre = txtnombre;
	}

	public void setTxtcodigo(JTextField txtcodigo) {
		this.txtcodigo = txtcodigo;
	}
	
	

}
